package com.huyn.baseframework.dynamicload;

import android.content.Context;

import com.huyn.baseframework.dynamicload.video.DLVideoLayout;
import com.ryg.dynamicload.internal.DLPluginPackage;

import java.lang.reflect.Constructor;

/**
 * Created by huyaonan on 16/5/10.
 */
public class DynamicLoaderHelper {

    public static DLLayout loadTargetLayout(Context context, DLPluginPackage plgPgk, String targetViewFullName) {
        try {
            Class<?> localClass = plgPgk.classLoader.loadClass(targetViewFullName);
            Constructor<?> localConstructor = localClass.getConstructor(new Class[]{Context.class});
            DLLayout instance = (DLLayout) localConstructor.newInstance(new Object[]{context});
            instance.injectPluginPackage(plgPgk);
            return instance;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static DLViewGroup loadTargetViewGroup(Context context, DLPluginPackage plgPgk, String targetViewFullName) {
        try {
            Class<?> localClass = plgPgk.classLoader.loadClass(targetViewFullName);
            Constructor<?> localConstructor = localClass.getConstructor(new Class[]{Context.class});
            DLViewGroup instance = (DLViewGroup) localConstructor.newInstance(new Object[]{context});
            instance.injectPluginPackage(plgPgk);
            return instance;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static DLContentView loadContentView(Context context, DLPluginPackage plgPgk, String targetViewFullName) {
        try {
            Class<?> localClass = plgPgk.classLoader.loadClass(targetViewFullName);
            Constructor<?> localConstructor = localClass.getConstructor(new Class[]{Context.class});
            DLContentView instance = (DLContentView) localConstructor.newInstance(new Object[]{context});
            instance.injectPluginPackage(plgPgk);
            return instance;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static DLVideoLayout loadVideoLayout(Context context, DLPluginPackage plgPgk, String targetViewFullName) {
        try {
            Class<?> localClass = plgPgk.classLoader.loadClass(targetViewFullName);
            Constructor<?> localConstructor = localClass.getConstructor(new Class[]{Context.class});
            DLVideoLayout instance = (DLVideoLayout) localConstructor.newInstance(new Object[]{context});
            instance.injectPluginPackage(plgPgk);
            return instance;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static DLClassInflate loadTargetClass(Context context, DLPluginPackage plgPgk, String targetViewFullName) {
        try {
            Class<?> localClass = plgPgk.classLoader.loadClass(targetViewFullName);
            Constructor<?> localConstructor = localClass.getConstructor(new Class[]{Context.class});
            DLClassInflate instance = (DLClassInflate) localConstructor.newInstance(new Object[]{context});
            return instance;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static DLView loadTargetView(Context context, DLPluginPackage plgPgk, String targetViewFullName) {
        try {
            Class<?> localClass = plgPgk.classLoader.loadClass(targetViewFullName);
            Constructor<?> localConstructor = localClass.getConstructor(new Class[] {Context.class});
            DLView instance = (DLView) localConstructor.newInstance(new Object[] {context});
            instance.injectPluginPackage(plgPgk);
            return instance;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
